package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupPosition;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.FileAdapter;
import flc.ast.bean.MyFileBean;
import flc.ast.databinding.ActivityMoveBinding;
import flc.ast.dialog.EditDialog;
import flc.ast.dialog.SortDialog;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.MimeUtils;
import stark.common.basic.utils.UriUtil;

public class MoveActivity extends BaseAc<ActivityMoveBinding> {

    public static String sPath;
    private List<MyFileBean> mFileBeanList = new ArrayList<>();
    private FileAdapter fileAdapter;
    private String mCurrentRootPath;
    private BasePopupView sortPopup;
    private SortDialog sortDialog;
    private int sortBy;
    private boolean isOrder;

    @Override
    protected int onCreate() {
        return R.layout.activity_move;
    }

    @Override
    public void onBackPressed() {
        if (mCurrentRootPath.equals(Environment.getExternalStorageDirectory().getPath())) {
            super.onBackPressed();
        } else {
            mCurrentRootPath = FileUtils.getDirName(mCurrentRootPath);
            mCurrentRootPath = mCurrentRootPath.substring(0, mCurrentRootPath.length() - 1);
            getMoveData(mCurrentRootPath);
        }
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivMoveBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.llSortType.setOnClickListener(this);
        mDataBinding.ivSort.setOnClickListener(this);
        mDataBinding.ivConfirm.setOnClickListener(this);

        mDataBinding.rvFileList.setLayoutManager(new LinearLayoutManager(mContext));
        fileAdapter = new FileAdapter();
        mDataBinding.rvFileList.setAdapter(fileAdapter);
        fileAdapter.addChildClickViewIds(R.id.ivFileDetails);
        fileAdapter.setOnItemClickListener(this);
        fileAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.llSortType) { //排序顺序
            isOrder = !isOrder;
            mDataBinding.ivSortType.setImageResource(isOrder ? R.drawable.xxjt : R.drawable.xsjt);
            sortFileBeanList();
        } else if (id == R.id.ivSort) { //排序
            showSortDialog();
        } else if (id == R.id.ivConfirm) {  //  确认
            cut();
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        String path = fileAdapter.getItem(position).getPath();
        if (FileUtils.isDir(path)) {
            mCurrentRootPath = path;
            getMoveData(mCurrentRootPath);
        }
    }

    @Override
    protected void initData() {
        mCurrentRootPath = Environment.getExternalStorageDirectory().getPath();
        getMoveData(mCurrentRootPath);
        mDataBinding.tvSortType.setText(getString(R.string.default_sorting));
    }

    private void getMoveData(String rootPath) {
        if (mCurrentRootPath.equals(Environment.getExternalStorageDirectory().getPath())) {
            mDataBinding.tvMoveTitle.setText(getString(R.string.sel_directory));
            mDataBinding.ivConfirm.setVisibility(View.GONE);
        } else {
            mDataBinding.tvSortType.setText(FileUtils.getFileName(rootPath));
            mDataBinding.ivConfirm.setVisibility(View.VISIBLE);
        }
        List<File> files = FileUtils.listFilesInDir(rootPath);
        if (files.size() == 0) {
            mDataBinding.ivFileNoData.setVisibility(View.VISIBLE);
            mDataBinding.rvFileList.setVisibility(View.GONE);
        } else {
            mDataBinding.ivFileNoData.setVisibility(View.GONE);
            mDataBinding.rvFileList.setVisibility(View.VISIBLE);
            mFileBeanList.clear();
            for (File file : files) {
                mFileBeanList.add(new MyFileBean(file.getPath(), file.getName(), file.length(),
                        FileUtils.getFileLastModified(file) / 1000L));
            }
            mDataBinding.rvFileList.scrollToPosition(0);
            sortFileBeanList();
        }
    }

    private void showSortDialog() {
        if (sortDialog == null) sortDialog = new SortDialog(mContext);
        sortPopup = new XPopup.Builder(mContext).popupPosition(PopupPosition.Bottom) //手动指定弹窗的位置
                .isDestroyOnDismiss(false) //对于只使用一次的弹窗，推荐设置这个
                .atView(mDataBinding.ivSort).hasShadowBg(false) // 去掉半透明背景
                .asCustom(sortDialog);
        sortDialog.setListener(new SortDialog.Ilistener() {

            @Override
            public void click(int position, String name) {
                sortBy = position;
                mDataBinding.tvSortType.setText(name);
                sortFileBeanList();
            }
        });
        sortPopup.show();
    }

    private void sortFileBeanList() {
        switch (sortBy) {
            case 0:
                fileAdapter.setList(mFileBeanList);
                if (isOrder) {
                    Collections.reverse(fileAdapter.getData());
                }
                break;
            case 1:
                // 按名称排序
                if (isOrder) {
                    Collections.sort(fileAdapter.getData(), (f1, f2) -> f1.getName().compareToIgnoreCase(f2.getName()));
                } else {
                    Collections.sort(fileAdapter.getData(), (f1, f2) -> f2.getName().compareToIgnoreCase(f1.getName()));
                }
                break;
            case 2:
                // 按时间排序
                if (isOrder) {
                    Collections.sort(fileAdapter.getData(), (f1, f2) -> Long.compare(f1.getDateModified(), f2.getDateModified()));
                } else {
                    Collections.sort(fileAdapter.getData(), (f1, f2) -> Long.compare(f2.getDateModified(), f1.getDateModified()));
                }
                break;
            case 3:
            case 4:
                // 按文件大小排序
                // 按类型排序
                if (isOrder) {
                    Collections.sort(fileAdapter.getData(), (f1, f2) -> Long.compare(f1.getSize(), f2.getSize()));
                } else {
                    Collections.sort(fileAdapter.getData(), (f1, f2) -> Long.compare(f2.getSize(), f1.getSize()));
                }
                break;
            default:
                break;
        }
        fileAdapter.notifyDataSetChanged();
    }

    private void cut() {
        FileUtils.move(sPath, mCurrentRootPath + "/" + FileUtils.getFileName(sPath));
        ToastUtils.showShort(R.string.cut_suc);
        finish();
    }

}